package org.yoao.crm.business.impl;

import java.util.List;

import org.yoao.crm.business.service.IMenuService;
import org.yoao.crm.dao.MenuDAO;
import org.yoao.crm.struts.form.MenuForm;
import org.yoao.crm.tools.CodeFactory;
import org.yoao.crm.tools.TimeTool;
import org.yoao.crm.tree.StringUtil;

/**
 * @author yxwonline.com
 * @description
 * @date 2010-7-21 下午03:34:06 
 */
public class MenuServiceImpl implements IMenuService {
	private MenuDAO menuDAO = MenuDAO.getMenuDAO();
	
	public MenuForm init(MenuForm form) throws Exception{		
		form.setKeyName(getUUID());
		form.setIsActive(1);
		form.setCreateTime(TimeTool.getSystemTime());		
		form.setSortNumber(form.getSortNumber()+1);
		form.setTarget("main");
		return form;
	}
	
	public MenuForm initModify(String keyName) throws Exception{
		return menuDAO.findMenuByKey(keyName);
	}
	
	public void save(MenuForm form) throws Exception {
		if(menuDAO.exist(form.getKeyName()))
			menuDAO.modify(form);
		else menuDAO.save(form);
	}
	
	private String getUUID() throws Exception{
		String uuid = CodeFactory.getUUID();
		MenuForm menu = menuDAO.findMenuByKey(uuid);
		if(menu == null) return uuid;
		else return getUUID();
	}
	
	/**@author yxwonline.com
	 * @description 
	 * @date 2010-7-22 下午04:10:22
	 */
	public boolean saveAll(String firstItemKey, String viewTurn){
		try{
			boolean flag = false;
			StringUtil util = new StringUtil();
			if(viewTurn != null && !viewTurn.equals("")) {
				List list = util.split(viewTurn);
				if(list != null) {
					flag = save(list, firstItemKey);
				} 
			}
			return flag;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	private boolean save(List list, String firstItemKey){
		try{
			if(list == null) return false;
			int result = menuDAO.setIsFirstItem(0, 1);
			if(result >= 0){
				int size = list.size();
				for(int i=0;i<size;i++) {
					MenuForm menuItem = (MenuForm)list.get(i);
					if(menuItem == null) continue;						
					MenuForm temp = menuDAO.findMenuByKey(menuItem.getKeyName());
					if(temp != null) {
						temp.setSortNumber(i);
						temp.setLayer(menuItem.getLayer());
						temp.setIsActive(menuItem.getIsActive());
						if(temp.getKeyName().equals(firstItemKey))
							temp.setIsFirstItem(1);
						else
							temp.setIsFirstItem(0);
						menuDAO.modify(temp);
					}				
				}
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public boolean delete(String keyName) {
		if(menuDAO.delete(keyName) > -1)
			return true;
		else return false;
	}

	public MenuForm getMenuFormByKey(String key) {
		try{
			return menuDAO.findMenuByKey(key);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public boolean copy(String keyName) {
		try {
			MenuForm menu = menuDAO.findMenuByKey(keyName);
			String oldKey = menu.getKeyName();
			menu.setTitle("[复制]"+menu.getTitle());
			menu.setKeyName(getUUID());
			menu.setCreateTime(TimeTool.getSystemTime());
			menu.setSortNumber(menu.getSortNumber()+1);
			if(menu.getIsFirstItem() == 1) menu.setIsFirstItem(0);
			return menuDAO.save(menu);			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}